package com.iori.common.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.ResourceBundle;

import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;

/**
 * 属性ファイルツールクラス
 *
 * @author Founder
 *
 */
public class PropertiesUtil {
	private final static String PROPERTIESFILE = "root.properties";

	private static Properties props = new Properties();

	/**
	 * ログ出力用。
	 */
	private static FounderLog log = LogFactory
			.getFounderLog(PropertiesUtil.class);

	/**
	 * 初始化
	 *
	 */
	public static void init() {

	}

	/**
	 * load properties文件内容
	 */
	static {
		try {
			Properties propsMain = new Properties();
			InputStream is = null;
			is = getResourceAsStream(PROPERTIESFILE);
			propsMain.load(is);
			is.close();

			Iterator keyIter = propsMain.keySet().iterator();
			while (keyIter.hasNext()) {
				ResourceBundle bundle = ResourceBundle.getBundle(propsMain
						.getProperty((String) keyIter.next()));
				for (Enumeration e = bundle.getKeys(); e.hasMoreElements();) {
					String key = (String) e.nextElement();
					if (!props.containsKey(key)) {
						props.put(key, bundle.getString(key));
						if (log.isDebugEnabled()) {
							Object msg = "===already load key：" + key + "===";
							log.debug(msg);
						}
					} else {
						throw new Exception("key：" + key + " already exist");
					}
				}
			}
		} catch (IOException e) {
			log.error(e);

		} catch (Exception e) {
			log.error(e);
		}
	}

	/**
	 * 根据参数resource获得相应xml配置文件的流
	 *
	 * @param resource
	 *            需要读取的xml配置文件的名称
	 * @return 对应配置文件的流
	 */
	private static InputStream getResourceAsStream(String resource) {
		InputStream stream = null;
		String stripped = resource.startsWith("/") ? resource.substring(1)
				: resource;

		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader != null) {
			stream = classLoader.getResourceAsStream(stripped);
		}
		if (stream == null) {
			throw new RuntimeException(resource + " not found");
		}
		return stream;
	}

	/**
	 * 属性ファイルより値を読み取る
	 *
	 * @param key
	 *            属性ファイルキー値
	 * @return キーに対応するvalue
	 * @throws BetyUtilException
	 */
	public static String getMessage(String key) {

		return props.getProperty(key);

	}

	/**
	 * 根據msgID和需要替換的内容得到message
	 *
	 * @param msgID
	 * @param values
	 *            需要替換内容數組
	 * @return
	 */
	public static String getMessage(String msgID, Object... values) {

		String originalString = props.getProperty(msgID);

		for (int i = 0; i < values.length; i++) {
			originalString = originalString.replaceFirst("\\{" + i + "\\}",
					values[i].toString());
		}
		return originalString;
	}

	public static void setMessage(String key, String value) {
		props.setProperty(key, value);
	}
	
	/**
	 * ファイルパスでPropertiesファイルを読み取り
	 *
	 * @param path 
	 * 			   ファイルパス
	 * @return props
	 * 			   Propertiesファイル
	 */
	public static Properties getPropertiesByPath(String path) {

		Properties props = null;
		FileInputStream is = null;
		try {
			props = new Properties();
			is = new FileInputStream(path);

			props.load(is);
			is.close();

		} catch (IOException e) {
			log.error(e);

		} catch (Exception e) {
			log.error(e);
		}

		return props;
	}

}
